﻿using BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;


namespace System.Web.Mvc
{
    public static class BLLHelper
    {
        public static MvcHtmlString B_DropDownList(this HtmlHelper helper, string codeName, string id)
        {
            var builder = new TagBuilder("select");
            builder.MergeAttribute("name", id);
            builder.MergeAttribute("id", id);
            builder.MergeAttribute("qn", id);
            builder.MergeAttribute("lay-search", "");
            builder.MergeAttribute("class", "layui-select");
            var list = new Codes().SelectCodeList(codeName);

            TagBuilder itemTag = new TagBuilder("option");
            itemTag.SetInnerText("-请选择" + codeName + "-");
            itemTag.MergeAttribute("value", "");
            builder.InnerHtml += itemTag.ToString();


            foreach (var item in list)
            {
                itemTag = new TagBuilder("option");
                itemTag.SetInnerText(item.itemText);
                itemTag.MergeAttribute("value", item.itemValue);
                builder.InnerHtml += itemTag.ToString();
            }
            builder.ToString(TagRenderMode.SelfClosing);
            return MvcHtmlString.Create(builder.ToString());
        }

        public static MvcHtmlString B_CheckBoxList(this HtmlHelper helper, string codeName, string id, bool isOriginal)
        {
            string builder = "";
            var list = new Codes().SelectCodeList(codeName);
            TagBuilder itemTag;
            foreach (var item in list)
            {
                itemTag = new TagBuilder("input");
                itemTag.MergeAttribute("type", "checkbox");
                itemTag.MergeAttribute("title", item.itemText);
                itemTag.MergeAttribute("name", id + "|" + item.itemValue);
                if (isOriginal)
                {
                    itemTag.MergeAttribute("lay-skin", "primary");
                }
                builder += itemTag.ToString();
            }
            return MvcHtmlString.Create(builder);
        }

        public static MvcHtmlString B_RadioButtonList(this HtmlHelper helper, string codeName, string id)
        {
            string builder = "";
            var list = new Codes().SelectCodeList(codeName);
            TagBuilder itemTag;
            foreach (var item in list)
            {
                itemTag = new TagBuilder("input");
                itemTag.MergeAttribute("type", "radio");
                itemTag.MergeAttribute("title", item.itemText);
                itemTag.MergeAttribute("name", id);
                itemTag.MergeAttribute("value", item.itemValue);                
                builder += itemTag.ToString();
            }
            return MvcHtmlString.Create(builder);
        }

        public static MvcHtmlString B_Code(this HtmlHelper helper, string codeName, string id)
        {
            var list = new Codes().SelectCodeList(codeName);
            string ret = "<script>var " + id + "Arr = " + LBCMS.Helper.JsonHelper.toJson(list) + "</script>";
            ret += "<script type=\"text/html\" id=\"" + id + "Tpl\">";
            ret += "{{# var item = " + id + "Arr.find((n)=>n.itemValue==d." + id + "); }}";
            ret += "{{#if(item){}}";
            ret += "{{=item.itemText}}";
            ret += "{{#  } }}";
            ret += "</script>";
            return MvcHtmlString.Create(ret);
        }


    }
}